﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UtilZ.Dotnet.Ex.Communication.Net.Ftp
{
    /// <summary>
    /// ftp用户信息
    /// </summary>
    public class FtpUserInfo
    {
        private string _userName = null;
        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName
        {
            get { return _userName; }
            set { _userName = value; }
        }

        private string _password = null;
        /// <summary>
        /// 密码
        /// </summary>
        public string Password
        {
            get { return _password; }
            set { _password = value; }
        }

        /// <summary>
        /// 用户访问策略
        /// </summary>
        public FtpAccessPolicy AccessPolicy { get; set; } = FtpAccessPolicy.ReadAndWrite;


        private List<LocalPathItem> _virtualPathItems = null;
        /// <summary>
        /// 用户目录列表
        /// </summary>
        public List<LocalPathItem> VirtualPathItems
        {
            get { return _virtualPathItems; }
            set { _virtualPathItems = value; }
        }

        private readonly Dictionary<string, DirectoryInfo> _pathDic = new Dictionary<string, DirectoryInfo>();
        internal IReadOnlyDictionary<string, DirectoryInfo> PathDic
        {
            get { return _pathDic; }
        }


        /// <summary>
        /// 构造函数
        /// </summary>
        public FtpUserInfo()
        {

        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        public FtpUserInfo(string userName, string password)
        {
            this._userName = userName;
            this._password = password;
        }

        /// <summary>
        /// 追加根目录列表
        /// </summary>
        /// <param name="rootPathItems"></param>
        internal void AppendPath(List<LocalPathItem> rootPathItems)
        {
            var pathItems = new List<LocalPathItem>();

            if (rootPathItems != null && rootPathItems.Count > 0)
            {
                pathItems.AddRange(rootPathItems);
            }

            if (this._virtualPathItems != null && this._virtualPathItems.Count > 0)
            {
                pathItems.AddRange(this._virtualPathItems);
            }

            if (pathItems.Count == 0)
            {
                throw new ArgumentException($"用户\"{this._userName}\"未指定指定可以访问的路径.");
            }

            string key;

            foreach (var pathItem in pathItems)
            {
                if (string.IsNullOrWhiteSpace(pathItem.Alias))
                {
                    throw new ArgumentException("ftp路径名称不能为null或空或全空格.");
                }

                key = pathItem.Alias.Trim().ToLower();
                if (this._pathDic.ContainsKey(key))
                {
                    throw new ArgumentException($"重复的ftp路径名称\"{pathItem.Alias}\".");
                }

                this._pathDic.Add(key, new DirectoryInfo(pathItem.Path));
            }
        }


    }
}
